﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Repository;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Controllers
{
    public class TestController : Controller
    {
        private GameSchoolWebContext _db = new GameSchoolWebContext();
        private GameRepository _game = new GameRepository();
        private TaskRepository _task = new TaskRepository();

		/// <summary>
		/// Gets the specified level and assigns that levelId to the test 
		/// </summary>
		/// <param name="levelId"></param>
		/// <returns></returns>
        [HttpGet]
        public ActionResult Create(int levelId)
        {
            Level level = _game.GetLevelById(levelId);
            return View(new Test { Level = level, LevelId = levelId });
        }

		/// <summary>
		/// Sends a test into the controller and adds that test to the database
		/// </summary>
		/// <param name="test"></param>
		/// <returns></returns>
        [HttpPost]
        public ActionResult Create(Test test)
        {
            Person author = _db.Persons.Where(p => p.UserName == User.Identity.Name).SingleOrDefault();
            test.AuthorId = author.Id;
            
            if (ModelState.IsValid)
            {
                _task.AddTest(test);
                return RedirectToAction("Details", "Level", new { id = test.LevelId });
            }
            return View(test);            
        }

		/// <summary>
		/// Gets a test by id
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet]
        public ActionResult Details(int id)
        {
            Test test = _task.GetTestById(id);
            return View(test);
        }

		/// <summary>
		/// Hands in test
		/// </summary>
		/// <param name="testId"></param>
		/// <returns></returns>
        [HttpGet]
        public ActionResult HandIn(int testId)
        {
            Test test = _task.GetTestById(testId);
            Person student = _db.Persons.Where(p => p.UserName == User.Identity.Name).SingleOrDefault();
            StudentTestPoints handIn = _task.GetStudentTestPoints(test, student);
            if (handIn == null)
            {
                handIn = new StudentTestPoints { Test = test, TestId = testId, Student = student, StudentId = student.Id, StudentAnswers = new List<StudentAnswer>() };
                foreach (var question in test.Questions)
                {
                    handIn.StudentAnswers.Add(new StudentAnswer { Question = question, QuestionId = question.QuestionId });
                }
            }
            ViewBag.TimeLeft = handIn.LevelEntry.StartedAt.AddDays(handIn.Test.Days).Subtract(DateTime.Now);
            return View(handIn);
        }

		/// <summary>
		/// Sends the test into the database
		/// </summary>
		/// <param name="handIn"></param>
		/// <returns></returns>
        [HttpPost]
        public ActionResult HandIn(StudentTestPoints handIn)
        {
            if (ModelState.IsValid)
            {
                handIn.StudentId = _db.Persons.Where(p => p.UserName == User.Identity.Name).SingleOrDefault().Id;
                _task.AddStudentTestPoints(handIn);
                return RedirectToAction("HandIn", new { testId = handIn.TestId });
            }
            return View(handIn);
        }

		/// <summary>
		/// Shows the student Grade 
		/// </summary>
		/// <param name="handInId"></param>
		/// <returns></returns>
        [HttpGet]
        public ActionResult Grade(int handInId)
        {
            StudentTestPoints handIn = _task.GetStudentTestPoints(handInId);
            return View(handIn);
        }

		/// <summary>
		/// Send the student grade into the repository
		/// </summary>
		/// <param name="grade"></param>
		/// <returns></returns>
        [HttpPost]
        public ActionResult Grade(StudentTestPoints grade)
        {
            if (ModelState.IsValid)
            {
                _task.UpdateStudentTestPoints(grade);
                return RedirectToAction("Grade", new { handInId = grade.Id });
            }
            return View(grade);
        }

    }
}
